const Common = require('../util/common')
const Constant = require('../constant/constant')
const moment = require('moment');
const ShareModel = require('../model/share');
const UserModel = require('../model/user')
const RemarkModel = require('../model/remark')
const sequelize = require('sequelize');
const LikeModel = require('../model/like');
const CollectModel = require('../model/collect');
const { Op } = require("sequelize");
const db = require('../db');



let exportObj = {
    userPage,
    remarkPage,
    sharePage,
    frozenUser,
    frozenUserOut,
    deleteShare,
    deleteRemark
};
// 导出对象，供其它模块调用
module.exports = exportObj;

//分页获取用户
async function userPage(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.query, ['page', 'limit']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            let result = await UserModel.findAndCountAll({
                offset: (req.query.page - 1) * req.query.limit,
                limit: parseInt(req.query.limit),
            })
            resObj.data = result
            res.json(resObj)
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//分页获取动态
async function sharePage(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.query, ['page', 'limit']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            let result = await ShareModel.findAndCountAll({
                offset: (req.query.page - 1) * req.query.limit,
                limit: parseInt(req.query.limit),
            })
            resObj.data = result
            res.json(resObj)
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//分页获取评论
async function remarkPage(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.query, ['page', 'limit']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            let result = await RemarkModel.findAndCountAll({
                offset: (req.query.page - 1) * req.query.limit,
                limit: parseInt(req.query.limit),
            })
            resObj.data = result
            res.json(resObj)
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//冻结用户
async function frozenUser(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.body, ['u_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            let result = await UserModel.update({
                frozen: 1
            }, {
                where: {
                    id: req.body.u_id
                }
            })
            if (result) {
                res.json(resObj)
            } else {
                res.json(Constant.DEFAULT_ERROR)
            }
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//用户解冻
async function frozenUserOut(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.body, ['u_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            let result = await UserModel.update({
                frozen: 0
            }, {
                where: {
                    id: req.body.u_id
                }
            })
            if (result) {
                res.json(resObj)
            } else {
                res.json(Constant.DEFAULT_ERROR)
            }
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//删除动态
async function deleteShare(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.body, ['s_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                let result1 = await ShareModel.destroy({
                    where: {
                        id: req.body.s_id
                    }
                }, { transaction: t })
                let result2 = await LikeModel.destroy({
                    where: {
                        s_id: req.body.s_id
                    }
                }, { transaction: t })
                let result3 = await RemarkModel.destroy({
                    where: {
                        s_id: req.body.s_id
                    }
                }, { transaction: t })
                let result4 = await CollectModel.destroy({
                    where: {
                        s_id: req.body.s_id
                    }
                }, { transaction: t })
                if (result1) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}
//删除评论
async function deleteRemark(req, res) {
    if(req.user.admin != 1){
        res.json(Constant.NO_ADMIN)
    }
    // 定义一个返回对象
    if (Common.check(req.body, ['r_id']) === false) {
        res.json(Constant.LACK)
    } else {
        const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
        try {
            return await db.transaction(async (t) => {
                let result1 = await RemarkModel.destroy({
                    where: {
                        id: req.body.r_id
                    }
                }, { transaction: t })
                let result2 = await LikeModel.destroy({
                    where: {
                        r_id: req.body.r_id
                    }
                }, { transaction: t })
                if (result1) {
                    res.json(resObj)
                } else {
                    res.json(Constant.DEFAULT_ERROR)
                }
            })
        } catch (error) {
            console.log(error);
            res.json(Constant.DEFAULT_ERROR)
        }
    }
}